#include "MainHeader.h"



Scroller::Scroller()
{
   ;
}

Scroller::~Scroller()
{
   ;
}

LRESULT CALLBACK ScrollProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   int             i = 0;
   char            buf[100];
   HWND            wnd;

   switch (message)
   {

   case WM_LBUTTONDOWN:
      for (i = 0; i < scroll.Number + 2; i++)
      {
         wnd = hScroll[i];
         if (wnd == hWnd)
         {
            CurObjT = dScroll;
            CurObjNo = i;
            break;
         }
      }
      // lblObjT Object Type
      SetWindowText(GetDlgItem(Main, lblObjT), "Object Type: Scroll");
      sprintf(buf, "Object #:: %d", CurObjNo);
      SetWindowText(GetDlgItem(Main, lblObjNo), buf);

      break;
   case WM_MOUSEMOVE:

      break;
   case WM_PAINT:

      break;
   case WM_DESTROY:

      break;
   }

   return DefWindowProc(hWnd, message, wParam, lParam);

}



int             DrawTileRect(HDC hdc, MousePointer mpointer, int mul)
{
   RECT            myrect;
   HBRUSH          curbrush = CreateSolidBrush(RGB(255, 0, 0));

   myrect.left = mpointer.sX * mul;
   myrect.top = mpointer.sY * mul;
   myrect.right = mpointer.Width * mul + myrect.left;
   myrect.bottom = myrect.top + mpointer.Height * mul;
   FrameRect(hdc, &myrect, curbrush);
   DeleteObject(curbrush);
   return 0;
}

LRESULT CALLBACK InputBoxProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

   int             choice = -1;
   char            buf[200];

   switch (message)
   {

   case WM_SHOWWINDOW:
      SetWindowText(GetDlgItem(hWnd, lblEnter), procstring);
      break;

   case WM_COMMAND:
      if (LOWORD(wParam) == cmdInputOK)
      {
         GetWindowText(GetDlgItem(hWnd, txtInput), buf, 4);
         sscanf(buf, "%x", &choice);
         ShowWindow(hWnd, SW_HIDE);

         EndDialog(hWnd, choice);
      }
      break;

   }

   return DefWindowProc(hWnd, message, wParam, lParam);
}

int             LoadInput(long val)
{
   int             blah;

   blah = DialogBox(hGlobal, MAKEINTRESOURCE(frmInputBox), 0, (int (__stdcall *) (struct HWND__ *, unsigned int, unsigned int, long)) InputBoxProc);
   if (blah > -1)
      return blah;
   return val;
}


int             GimmeAdoor(int X, int Y)
{
   int             i = 0;
   int             door = 0;
   int             curX = 0;
   int             curY = 0;
   int             curH = 0;
   int             curW = 0;

   // Function returns what door the is touching
   for (i = 0; i < DoorBuf.DoorCount; i++)
   {

      if (Doors[DoorBuf.DoorNotes[i]].OwnerRoom == Combos[cRoom].GetListIndex())
      {
         curX = Doors[DoorBuf.DoorNotes[i]].XEntrance;
         curY = Doors[DoorBuf.DoorNotes[i]].YEntrance;
         curH = (Doors[DoorBuf.DoorNotes[i]].DHeight); // -Doors[DoorBuf.DoorNotes[i]].YEntrance)+1;

         curW = (Doors[DoorBuf.DoorNotes[i]].DWidth); // -Doors[DoorBuf.DoorNotes[i]].XEntrance)+1;
         // ShowWindow((HWND)hDoors[i],SW_SHOW);
         // MoveWindow((HWND)hDoors[i],(u16)(curX-minX)*16,(u16)(curY-minY)*16,(curW+1)*16,(curH-curY+1)*16,0);

         if ((curX <= X) &&
             (curW >= X) &&
             (curY <= Y) &&
             (curH >= Y))
         {

            return i;
         }
      }
   }
   return -1;
}

int             Findmeascroll(int X, int Y)
{
   int             scrollnum = 0;
   int             MainX = 0;
   int             MainY = 0;
   int             MainH = 0;
   int             MainW = 0;
   int             curX = 0;
   int             curY = 0;
   int             curH = 0;
   int             curW = 0;
   int             i = 0;
   int             j = 0;
sctype thisScroll=scroll.Scrolls[cboScroll.GetListIndex()];
/*
 * Given an X,Y
 * This function will find what rectangle you are in, then will go deeper and find 
 * if said rectangle is intersecting this will take some time to code -.-
 */
  
if ((scroll.Room != 0xFF) && (thisScroll.xStart != 0xFF) && (thisScroll.yEnd != 0xFF)){
         
            MainX = thisScroll.xStart;
            MainY = thisScroll.yStart;
            MainW = thisScroll.xEnd;
            MainH = thisScroll.yEnd;
            if (((MainX) <= X) && ((MainW) >= X) &&
                ((MainY) <= Y) && ((MainH) >= Y))
            {
               
               return cboScroll.GetListIndex();
            }
         
      
   }
   return -1; // Nothing was found

}

int             Gimmeasprite(int X, int Y, int giveback)
{
   int             i = 0;
   int             x = 0;
   int             y = 0;
   int             width = 0;
   int             height = 0;

   switch ((u8) Combos[cSpriteSet].GetListIndex())
   {
   case 0:
      for (i = 0; i < Enemy1.max; i++)
      {

         width = OverallSize[(Enemy1.Enemies[i].Creature & 0xF) - 1].right - OverallSize[(Enemy1.Enemies[i].Creature & 0xF) - 1].left;

         height = OverallSize[(Enemy1.Enemies[i].Creature & 0xF) - 1].bottom - OverallSize[(Enemy1.Enemies[i].Creature & 0xF) - 1].top;
         x = Enemy1.Enemies[i].X;
         y = Enemy1.Enemies[i].Y;
         if (((x) <= X) &&
             ((x + width / 16) >= X) &&
             ((y) <= Y) &&
             ((y + height / 16) >= Y))
         {

            if (giveback)
            {
               MovingSprite[0] = 1;
               MovingSprite[1] = i;
            }
            return i;
         }
      }
      break;
   }

   return -1;
}

int             DrawClipIdent()
{
   int             currclip = Combos[cClip].GetListIndex();
   RECT            blah;
   int             thisX = 0;
   int             thisY = 0;
   int             x = 0;
   int             y = 0;

   HBRUSH          curbrush = CreateSolidBrush(RGB(255, 255, 255));

   for (thisY = 0; thisY < ThisLevelData->Y; thisY++)
   {
      for (thisX = 0; thisX < ThisLevelData->X; thisX++)
      {

         if (ThisClipData->TileBuf2D[thisX + (thisY * (ThisClipData->X))] == currclip)
         {

            blah.left = thisX * 16;
            blah.right = thisX * 16 + 16;
            blah.top = thisY * 16;
            blah.bottom = thisY * 16 + 16;

            FrameRect(ThisBackBuffer.DC(), &blah, curbrush);

         }
      }
   }
   DeleteObject(curbrush);
   InvalidateRect(hwndMap, 0, 1);
   return 0;
}

int             HandleMapLeftButton(UINT message, WPARAM wParam, LPARAM lParam)
{
	if(MP[1]==0){
		

		  // Hey there let's check our dimensions.
		   if (ScrollCheck.value() == 1){
		     DoScroll(lParam);
			 return 0;
		  } else 
			  if (ScrollCheck.value() == 0){
				 if (MovingSprite[0] == 0 && CheckBoxes[chkES].value() == 1){
				if (Gimmeasprite(((GetX(lParam) / 16) + nHScroll[sHMap]), ((GetY(lParam) / 16) + nVScroll[sVMap]), 1) != -1)
				{
				   return 0;
				}
				 }
			 if (MovingDoor[0] == 0 && CheckBoxes[chkED].value() == 1)
			 {
				MovingDoor[1] = GimmeAdoor(((GetX(lParam) / 16) + nHScroll[sHMap]), ((GetY(lParam) / 16) + nVScroll[sVMap]));
				if (MovingDoor[1] != -1)
				{
				   MovingDoor[0] = 1;
				   return 0;
				}
			 }
		 	if(wParam == (MK_LBUTTON| MK_SHIFT)){
					ClipBoard.CopyTiles(lParam);
					DrawRoom();
				    InvalidateRect(hwndMap,0,1);
					return 0;
			}
		
            EditLayers(wParam,lParam);
		  }
		  //DrawRoom();
		  InvalidateRect(hwndMap, 0, 1);
	   
	}
   return 0;
}


int             EditThisLayer(nRoomBuff * Layer, WPARAM wParam, LPARAM lParam, byte usens, u16 ToUse, int mul)
{ // Layer to use, XY value, if it's bigger then 16x16

   int             baseX = (GetX(lParam) / 16)+ nHScroll[sHMap];


   int             baseY = (GetY(lParam) / 16)+ nVScroll[sVMap];

   char buffer[256]={0};
   int             y = 0;
   int             x = 0;
   int             ny = 0;
   int             nx = 0;
   int             add = 0;
   int             mwidth = Layer->X;
   int			   undo=0;
   int			   cock=0;
   u16* TileBuf2D=&Layer->TileBuf2D[0];
   int             Tiles=(mpMap.uX)+(mpMap.uY) ;
   	int width=0,height=0;

     if (keyno==1){  
		y =  GetMessagePos();
        baseX = (GetX(y) / 16)+ nHScroll[sHMap];
		baseY = (GetY(y) / 16)+ nVScroll[sVMap];
		 RECT Drawme;
		 
		 switch(wParam){
		  case 0x5a:
			  sprintf(buffer,"Map-Undo's left: %d",Layer->UndoBuff.UndoNum);
			  SetWindowText(hwndMap,buffer);
			  undo=1;
          
              Layer->UndoBuff.Undo();


			  Drawme.top = 0;
			  Drawme.left =0;
			  Drawme.bottom = ThisLevelData->Y;
			  Drawme.right = ThisLevelData->X;
			  Layer->DrawRect.top = 0;
			  Layer->DrawRect.left =0;
			  Layer->DrawRect.bottom = ThisLevelData->Y;
			  Layer->DrawRect.right = ThisLevelData->X;

             
			  break;
		  case 67://'c'
			  SetWindowText(hwndMap,"Map-Pressed Ctrl + C");
			  Drawme.top =mpMap.sY;
			  Drawme.left = mpMap.sX ;
		      Drawme.bottom = Drawme.top + mpMap.Height;
			  Drawme.right = Drawme.left + mpMap.Width;
			  Layer->CopyData.Copy(Layer->X,Drawme,Layer->TileBuf2D);
            
			  break;

		  case 86://'p'
			  SetWindowText(hwndMap,"Map-Pressed Ctrl + P");
			  Layer->UndoBuff.Set(Layer->X*Layer->Y*2,(u16*)Layer->TileBuf2D);
              Layer->CopyData.Paste(&Layer->TileBuf2D[mpMap.sX+( mpMap.sY*Layer->X)]); //mpMap.sX+( mpMap.sY*Layer->X)]);
			  DrawAll=1;
			 /* Layer->DrawRect.top  = mpMap.sY;
	          Layer->DrawRect.left = mpMap.sX;
			  Layer->DrawRect.bottom = (mpMap.sY+( mpMap.Height));
			  Layer->DrawRect.right = ( mpMap.sX+  mpMap.Width  );*/

			  /*Layer->DrawRect.top = 0;
			  Layer->DrawRect.left =0;
			  Layer->DrawRect.bottom = ThisLevelData->Y;
			  Layer->DrawRect.right = ThisLevelData->X;*/
			 /* Layer->Dirty=Layer->SDirty=1; 
			   undo=0;
			  DrawRoom();
		      InvalidateRect(hwndMap, 0, 1);*/
			 
			 // return 0;
			  break;
			  
		 }
 
             keyno=0;
	 }else{ 
     /*Ideas
	  if map box is bigger than tileset box we loop.
	  if tileset box is bigger than mapbox we don't loop we just copy as many as possible
	  */
	/*	  nx=mpMap.Width;
		  ny=mpMap.Height;
		 for(x=0;x<mpMap.Width;x++){
			   for(y=0;y<mpMap.Height;y++){
				   TileBuf2D[(baseX + x - (x>mpTileset.Width ? nx:0)) + ( baseY + y-(y>mpTileset.Height ? ny:0)) * (mwidth)] = ToUse + (Layer==&ThisClipData ? 0:x + (y * 16));
			   }
		 }*/
	//Do some math to see how much we're looping
		 /*
	Layer->UndoBuff.Set(Layer->X*Layer->Y*2,Layer->TileBuf2D);//Set the undobuffer
		if(mpMap.Width > mpTileset.Width){
			//Determine the difference
			width=mpMap.Width -mpTileset.Width;	
		}
		
		if(mpMap.Height> mpTileset.Height){

		   height=mpMap.Height- mpTileset.Height;
		}
		for(ny=0;ny<height+1;ny++){
	                  for(nx=0;nx<width+1;nx++){
						
						  for (y = 0; y <  mpTileset.Height; y++)
						  {
					 
							if(ny*mpTileset.Height+y>mpMap.Height) break; 
							for (x = 0; x <  mpTileset.Width; x++)
							 { 
								   if(nx*mpTileset.Width + x  > mpMap.Width) break;
                                   TileBuf2D[(baseX + x ) + ( baseY + y) * (mwidth) + nx*mpTileset.Width + ny*mpTileset.Height *mwidth]  = ToUse + (Layer==&ThisClipData ? 0:x + (y * 16));
						
							
						     }

							

						}
						 
					  }
				}

		 */
		 #define min(a,b) (((a) < (b)) ? (a) : (b))
		#define max(a,b) (((a) > (b)) ? (a) : (b))

		 Layer->UndoBuff.Set(Layer->X*Layer->Y*2,(u16*)Layer->TileBuf2D);//Set the undobuffer

		width  = mpMap.Width; //max(   mpTileset.Width-mpMap.Width,  0);// + 1;
		height = mpMap.Height; //max(  mpTileset.Height-mpMap.Height, 0);// + 1;

		for(ny=0;ny<height;ny++){
			for(nx=0;nx<width;nx++){
				for (y = 0; y < mpTileset.Height; y++){
					if(ny*mpTileset.Height+y>=mpMap.Height) break;
					for (x = 0; x < mpTileset.Width; x++){
						if(nx*mpTileset.Width + x >= mpMap.Width) break;
						//ToUse is the tile to start at in the tile list
						TileBuf2D[(baseX + x ) + ( baseY + y) * (mwidth) + nx*mpTileset.Width + ny*mpTileset.Height *mwidth]
							= ToUse + (Layer==ThisClipData ? 0:x + (y * 16));
					}
				}
			}
		
		}
		
		
	}
	/* if(undo!=1){
        //Set Rect up
	   Layer->DrawRect.top = baseY;
	   Layer->DrawRect.left =  baseX;
	   Layer->DrawRect.bottom = (baseY+(mpTileset.Height) +height);
	   Layer->DrawRect.right = (baseX+ mpTileset.Width  + width);
	 }*/
	 		  /*Layer->DrawRect.top  = mpMap.sY;
	          Layer->DrawRect.left = mpMap.sX;
			  Layer->DrawRect.bottom = (mpMap.sY+( mpMap.Height)+ nVScroll[sVMap]);
			  Layer->DrawRect.right = ( mpMap.sX+  mpMap.Width+ nHScroll[sHMap]  );*/
	          Layer->Dirty=Layer->SDirty=1; 
		DrawRoom();
		//DrawLevel();
		SetWindowText(hwndMap,"Map");
		InvalidateRect(hwndMap,0,1);
      return 0;
   

 
}

int EditLayers(WPARAM wParam,LPARAM lParam){
    
	int i=0;
	int Layer[4]={0};

	

			 if (CheckBoxes[Foreground].value() == 1 || chkMC[0].value() == 1){
				EditThisLayer(ThisForeground,wParam, lParam, 1, Tile);
			 }
			 else 
			 if (CheckBoxes[Level].value() == 1 || chkMC[1].value() == 1){
				EditThisLayer(ThisLevelData,wParam,lParam, 1, Tile);
			 }
			 else 
			 if (CheckBoxes[Background].value() == 1 || chkMC[2].value() == 1){
				EditThisLayer(ThisBackLayer,wParam, lParam, 1, Tile);
				
			 }
			 else 
			 if (CheckBoxes[Clip].value() == 1 || chkMC[3].value() == 1){
				EditThisLayer(ThisClipData,wParam,lParam, 0, Combos[cClip].GetListIndex());
				
			 }/*

			 if (CheckBoxes[Foreground].value() == 1 || chkMC[0].value() == 1){
				i=0;
			 }
			 else 
			 if (CheckBoxes[Level].value() == 1 || chkMC[1].value() == 1){
				i=1;
			 }
			 else 
			 if (CheckBoxes[Background].value() == 1 || chkMC[2].value() == 1){
				i=2;
				
			 }
			 else 
			 if (CheckBoxes[Clip].value() == 1 || chkMC[3].value() == 1){
				i=3;
				
			 }
			
			 EditThisLayer(&Layers[i], 
						   lParam,  
						   (i==3 ? 0:1),
						   (i==3 ? Combos[cClip].GetListIndex():Tile)
						   );
			
			 
*/
return 0;
}
int DoScroll(LPARAM lParam){
	 if (!MovingScroll[0]){
				MovingScroll[1] = Findmeascroll(((GetX(lParam) / 16) + nHScroll[sHMap]), ((GetY(lParam) / 16) + nVScroll[sVMap]));

				if (MovingScroll[1]){
				   MovingScroll[0] = 1;

				}else{
				   MovingScroll[0] = MovingScroll[1] = 0;
				}
			 }
			 return 0;



}


